package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.AccidentTypeEntity;
import city.spring.modules.oldman.service.AccidentTypeService;
import city.spring.modules.personnel.entity.EmployeeEntity;
import city.spring.modules.personnel.service.EmployeeService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：意外类型
 *
 * @author pxm
 * @date 2020-06-28 10:12:00
 */
@RestController
@RequestMapping("/accident_type")
public class AccidentTypeController extends ApiController {
    private final AccidentTypeService accidentTypeService;
    private final EmployeeService employeeService;

    /**
     * 实体类可排序字段
     */
    private final List<SFunction<AccidentTypeEntity, ?>> entityOrderFields;

    public AccidentTypeController(AccidentTypeService accidentTypeService, EmployeeService employeeService) {
        this.accidentTypeService = accidentTypeService;
        this.employeeService = employeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(AccidentTypeEntity::getId);
        entityOrderFields.add(AccidentTypeEntity::getType);
        entityOrderFields.add(AccidentTypeEntity::getDescribes);
        entityOrderFields.add(AccidentTypeEntity::getRanks);
        entityOrderFields.add(AccidentTypeEntity::getTreatmentPlan);
        entityOrderFields.add(AccidentTypeEntity::getContacts);
        entityOrderFields.add(AccidentTypeEntity::getPhone);
        entityOrderFields.add(AccidentTypeEntity::getGmtCreate);
        entityOrderFields.add(AccidentTypeEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>意外类型</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, AccidentTypeEntity entity) {
        LambdaQueryChainWrapper<AccidentTypeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>意外类型</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, AccidentTypeEntity entity) {
        LambdaQueryChainWrapper<AccidentTypeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<AccidentTypeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (AccidentTypeEntity record : page.getRecords()) {
            try {
                EmployeeEntity employeeEntity = employeeService.getById(record.getContacts());
                record.setContactsName(employeeEntity.getName());
            } catch (Exception ignore) {
                record.setContacts(null);
                record.setContactsName("< 找不到人员 >");
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>意外类型</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(accidentTypeService.getById(id));
    }

    /**
     * 添加一个 <strong>意外类型</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody AccidentTypeEntity entity) {
        accidentTypeService.saveAccidentType(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>意外类型</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody AccidentTypeEntity entity) {
        entity.setId(id);
        accidentTypeService.updateAccidentType(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>意外类型</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        accidentTypeService.deleteAccidentType(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>意外类型</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        accidentTypeService.deleteAccidentType(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<AccidentTypeEntity> buildLambdaQuery(AccidentTypeEntity entity) {
        LambdaQueryChainWrapper<AccidentTypeEntity> lambdaQuery = accidentTypeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), AccidentTypeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), AccidentTypeEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescribes()), AccidentTypeEntity::getDescribes, entity.getDescribes());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRanks()), AccidentTypeEntity::getRanks, entity.getRanks());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTreatmentPlan()), AccidentTypeEntity::getTreatmentPlan, entity.getTreatmentPlan());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContacts()), AccidentTypeEntity::getContacts, entity.getContacts());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), AccidentTypeEntity::getPhone, entity.getPhone());
        lambdaQuery.eq(entity.getGmtCreate() != null, AccidentTypeEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, AccidentTypeEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<AccidentTypeEntity> buildQuery(AccidentTypeEntity entity) {
        QueryWrapper<AccidentTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getDescribes()), "describes", entity.getDescribes());
        wrapper.like(StringUtils.isNotBlank(entity.getRanks()), "ranks", entity.getRanks());
        wrapper.like(StringUtils.isNotBlank(entity.getTreatmentPlan()), "treatment_plan", entity.getTreatmentPlan());
        wrapper.like(StringUtils.isNotBlank(entity.getContacts()), "contacts", entity.getContacts());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "phone", entity.getPhone());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
